package com.lyshop.jinding.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.lyshop.jinding.R;
import com.lyshop.jinding.adapters.SignAdapter;
import com.lyshop.jinding.application.MyApplication;
import com.lyshop.jinding.bean.SignBean;
import com.lyshop.jinding.core.AppAction;
import com.lyshop.jinding.net.ActionCallbackListener;
import com.lyshop.jinding.utils.Utils;

import java.util.ArrayList;
import java.util.List;

public class SignDialog extends MyDialog implements View.OnClickListener {
    private static final String TAG = "SignDialog";
    private View rootView;
    private AppAction mAppAction;
    private View view_top;
    private TextView tv_describe;
    private MyGridView gv_sign;

    private SignAdapter mSignAdapter;
    private List<SignBean> mSignList = new ArrayList<>();
//    private SignListBean mBean;


    public SignDialog(Context context, boolean isCancelAble, int gravity) {
        super(context, isCancelAble, gravity);

        this.mContext = context;
        Window w = this.getWindow();
        if (w != null) {
            w.setWindowAnimations(R.style.dialogTranslateAnim);
        }
        rootView = LayoutInflater.from(context).inflate(R.layout.dialog_sign, null);
        setContentView(rootView);
        findViews(rootView);
        mAppAction = ((MyApplication) mContext.getApplicationContext()).getAppAction();
        initData();
    }

    private void initData() {
        mSignAdapter = new SignAdapter(mContext, mSignList);
        gv_sign.setAdapter(mSignAdapter);

        String title = "天天签到 送<font color=\"#477AE5\">大额积分</font>";
        Utils.setHtmlText(tv_describe,title);

    }

    @Override
    protected void onStart() {
        super.onStart();
        getSignList();
    }

    private void findViews(View rootView) {
        gv_sign = rootView.findViewById(R.id.gv_sign);
        tv_describe = rootView.findViewById(R.id.tv_describe);
        view_top = rootView.findViewById(R.id.view_top);
        view_top.setOnClickListener(this);
    }

    private void getSignList() {
        mAppAction = MyApplication.getApplication().getAppAction();
        mAppAction.getSignDataList(new ActionCallbackListener<List<SignBean>>() {
            @Override
            public void onSuccess(List<SignBean> data) {
                mSignList.clear();
                mSignList.addAll(data);
                mSignAdapter.notifyDataSetChanged();
            }
            @Override
            public void onFailure(String errorCode, String message) {

            }
        });
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.view_top:
                if(isCancelAble){
                    dismiss();
                }
                break;
        }
    }

}
